//use package
use std::io::prelude::*;
use std::net::TcpListener;

//main function
fn main() -> std::io::Result<()> {
    //bind to local port
    let listener = TcpListener::bind("127.0.0.1:8000")?;

    //deal with each request
    for stream in listener.incoming() {
        //deal with stream
        match stream {
            //if Ok
            Ok(mut stream) => {
				//send 
                stream.write(b"hello, guest")?;

				//vector for save message
                let mut mes = vec![0u8; 128];

				//read message
                stream.read(&mut mes).unwrap();

                //print
                println!("client message: {:?}", String::from_utf8_lossy(&mes).trim_end_matches('\0'));
            }
            //if Err
            Err(e) => {
                println!("build connect failed! {}", e);
            }
        }
    }
    //return
    Ok(())
}
